﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace CzJ_VoteSys.Models.Database
{
    public partial class CzJ_VoteSysContext : DbContext
    {
        public CzJ_VoteSysContext()
        {
        }

        public CzJ_VoteSysContext(DbContextOptions<CzJ_VoteSysContext> options)
            : base(options)
        {
        }

        public virtual DbSet<CompanyAward> CompanyAwards { get; set; }
        public virtual DbSet<Participation> Participations { get; set; }
        public virtual DbSet<ResourceGroup> ResourceGroups { get; set; }
        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<VoteRecord> VoteRecords { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=CzJ_VoteSys;Integrated Security=SSPI");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<CompanyAward>(entity =>
            {
                entity.HasKey(e => e.AwardId)
                    .HasName("PK__CompanyA__B08935DE4B5D1962");

                entity.HasOne(d => d.Participation)
                    .WithMany(p => p.CompanyAwards)
                    .HasForeignKey(d => d.ParticipationId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__CompanyAw__Parti__6EF57B66");

                entity.HasOne(d => d.ResourceGroup)
                    .WithMany(p => p.CompanyAwards)
                    .HasForeignKey(d => d.ResourceGroupId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__CompanyAw__Resou__6FE99F9F");
            });

            modelBuilder.Entity<Participation>(entity =>
            {
                entity.Property(e => e.VoteCount).HasDefaultValueSql("((0))");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Participations)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__Participa__UserI__68487DD7");
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.Property(e => e.LastVoteDate).HasDefaultValueSql("(getdate())");

                entity.Property(e => e.RoleType).HasDefaultValueSql("('customer')");
            });

            modelBuilder.Entity<VoteRecord>(entity =>
            {
                entity.HasOne(d => d.Participation)
                    .WithMany(p => p.VoteRecords)
                    .HasForeignKey(d => d.ParticipationId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__VoteRecor__Parti__6C190EBB");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.VoteRecords)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__VoteRecor__UserI__6B24EA82");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
